﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pibby.Core.IO;
using Pibby.Core.Persistence;

using dogd;

namespace Pibby.Core
{
	[Table("Episode")]
	public class Episode : Identifiable, /* IEquatable<Episode>, */ IEpisode
	{
		private Key ForeignKey;

		#region Constructors

		public Episode()
			: this(string.Empty, null)
		{
			ForeignKey = new Key();
		}

		public Episode(string name, Uri link)
			: this(name, link, string.Empty)
		{
		}

		public Episode(string name, Uri link, string id)
			: base(id)
		{
			this.Name = name;
			this.Feed = link;
		}

		#endregion

		public override string ToString()
		{
			if (string.IsNullOrEmpty(this.Name))
			{
				return this.Feed.AbsoluteUri;
			}

		    return string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0} : {1}", this.Name, this.Feed.AbsoluteUri);
		}

		#region Properties

		[PrimaryKeyColumn("EpisodeID")]
		public int DbKey
		{
			get { return this.PrimaryKey.Value; }
			set { this.PrimaryKey.Value = value; }
		}

		public bool IsDbKeyValid
		{
			get { return this.PrimaryKey.IsValid; }
		}

		[Column("Name")]
		public string Name
		{
		    get;
		    set;
		}

		[ForeignKeyColumn("SubscriptionID")]
		public int SubscriptionID
		{
			get { return this.ForeignKey.Value; }
			set { this.ForeignKey.Value = value; }
		}

		[Column("PodcastName")]
		public string PodcastName
		{
			get;
			set;
		}

		public Uri Feed
		{
		    get;
		    set;
		}

		[Column("FeedUrl")]
		public string FeedUrl
		{
			get
			{
				string url = string.Empty;

				if (this.Feed != null)
				{
					url = this.Feed.AbsoluteUri;
				}

				return url;
			}

			set
			{
				if (!string.IsNullOrEmpty(value))
				{
					this.Feed = new Uri(value);
				}
			}
		}

		public string FileName
		{
			get
			{
				return System.IO.Path.GetFileName(this.Feed.AbsoluteUri);
			}
		}

		public string SafeFileName
		{
			get
			{
				return FileNameSanitizer.FixWebLink(this.FileName);
			}
		}

		[DateTimeColumn("PublicationDate")]
		public DateTime PublicationDate
		{
			get;
			set;
		}

		[Column("Duration")]
		public string Duration
		{
			get;
			set;
		}

		[IntegerColumn("FileSize")]
		public long FileSize
		{
			get;
			set;
		}

		//public bool IsSelected
		//{
		//    get;
		//    set;
		//}

		public EpisodeAge Age
		{
			get;
			set;
		}

		[IntegerColumn("Age")]
		public int AgeValue
		{
			get { return (int)this.Age; }
			set { this.Age = (EpisodeAge)value; }
		}

		public EpisodeLifecycle Stage
		{
			get;
			set;
		}

		[IntegerColumn("Stage")]
		public int StageValue
		{
			get { return (int)this.Stage; }
			set { this.Stage = (EpisodeLifecycle)value; }
		}

		[Column("Description")]
		public string Description
		{
			get;
			set;
		}

		#endregion

		#region Columns Names

		public static string GuidColumn { get { return "Guid"; } }

		public static string NameColumn { get { return "Name"; } }

		public static string FeedUrlColumn { get { return "FeedUrl"; } }

		public static string PublicationDateColumn { get { return "PublicationDate"; } }

		public static string DurationColumn { get { return "Duration"; } }

		public static string FileSizeColumn { get { return "FileSize"; } }

		public static string PodcastNameColumn { get { return "PodcastName"; } }

		public static string AgeColumn { get { return "Age"; } }

		public static string StageColumn { get { return "Stage"; } }

		public static string DescriptionColumn { get { return "Description"; } }
		
		#endregion Column Names
	}
}
